package com.fingard.dsp.bank.directbank.psbc01;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 邮储直联余额查询
 * 
 * @author fanghh
 * @date 2018/7/4
 */
public class PSBC01Balance extends PSBC01Base implements IBalance {
	public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
		RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
		RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
		retAts.writeStart();
		
		try {
			PSBC01Header bankHeader = new PSBC01Header();
			bankHeader.OpName = "2000";
			bankHeader.OpDate = Format.Date8Format.format(new Date());
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHeader(xmlWriter, bankHeader);
			xmlWriter.writeStartElement("Param");
			String account = "";
			String currency = "";
			while (p_req7506.readRecord()) {
				account = p_req7506.readedDetail.actNum;
				currency = convertCurToBank(p_req7506.readedDetail.actCur);
				tmpRetDetail.actNum = account;
				tmpRetDetail.actCur = p_req7506.readedDetail.actCur;
				tmpRetDetail.actArea = p_req7506.readedDetail.actArea;
				
				xmlWriter.writeElementString("Query_Account", account);
				xmlWriter.writeElementString("Currency", currency);
				xmlWriter.writeElementString("Reserved1", "");
				xmlWriter.writeElementString("Reserved2", "");
				xmlWriter.writeElementString("Reserved3", "");
				xmlWriter.writeElementString("Reserved4", "");
			}
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRespCode = tmpBkReader.getFirstText("/root/Head/OpRetCode");
					if ("00".equals(tmpRespCode)) {
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpBkReader.getFirstText("/root/Head/OpRetMsg");
						if (tmpBkReader.read("/root/Param")) {
							tmpRetDetail.currBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("Balance"));
							tmpRetDetail.availBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("Usable_Balance"));
						}
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpBkReader.getFirstText("/root/Head/OpRetMsg");
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
			retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}
		
		retAts.writeEnd();
		return retAts;
	}

	@Override
	public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
		RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
		String tmpRespCode = "";
		String tmpRespInfo = "";
		retAts.writeStart();// 写返回ATS的统一头部信息
		try {
			Calendar tmpCalendarFrom = Calendar.getInstance();
			tmpCalendarFrom.setTime(p_req7505.getDateFrom());

			Calendar tmpCalendarTo = Calendar.getInstance();
			tmpCalendarTo.setTime(p_req7505.getDateTo());

			int tmpIntDateFrom = 0;
			int tmpIntDateTo = 0;
			// 按自然日循环查询
			while (tmpIntDateFrom <= tmpIntDateTo && tmpRespCode.length() == 0) {
				PSBC01Header bankHeader = new PSBC01Header();
				bankHeader.OpName = "2100";
				bankHeader.Outsys_Code = "";
				SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");// 设置日期格式
				bankHeader.OpDate = df.format(new Date()) + "";

				XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
				writeCommonHeader(xmlWriter, bankHeader);
				xmlWriter.writeStartElement("Param");
				xmlWriter.writeElementString("Query_Account", p_req7505.actNum);
				xmlWriter.writeElementString("Begin_Date", Format.Date8Format.format(tmpCalendarFrom.getTime()));
				xmlWriter.writeElementString("Currency", convertCurToBank(p_req7505.actCur));
				xmlWriter.writeElementString("Reserved1", "");
				xmlWriter.writeElementString("Reserved2", "");
				xmlWriter.writeElementString("Reserved3", "");
				xmlWriter.writeElementString("Reserved4", "");
				xmlWriter.writeEndElement();
				xmlWriter.writeEndDocument();

				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
				if (tmpStrRet[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					if (tmpBkReader != null) {
						String tmpRetCode = tmpBkReader.getFirstText("/root/Head/OpRetCode");
						String tmpRetMsg = tmpBkReader.getFirstText("/root/Head/OpRetMsg");
						if (tmpRetCode.equals("00")) {
							RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
							// 交易日期
							String tmpETYDAT = Format.Date8Format.format(tmpCalendarFrom.getTime());
							if (tmpETYDAT.length() == 8) {
								tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-"
										+ tmpETYDAT.substring(6, 8);
							} else {
								tmpRetDetail.balDate = tmpETYDAT;
							}
							// 余额
							tmpRetDetail.actBal = tmpBkReader.getFirstText("/root/Param/Balance");
							retAts.writeRetDetail(tmpRetDetail);
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = tmpRetMsg;
							break;
						}
					} else {
						tmpRespCode = "-1";
						tmpRespInfo = tmpSbMsg.toString();
						break;
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpStrRet[1];
					break;
				}
				tmpCalendarFrom.add(Calendar.DATE, 1);
				tmpIntDateFrom = Integer.parseInt(Format.Date8Format.format(tmpCalendarFrom.getTime()));
				tmpIntDateTo = Integer.parseInt(Format.Date8Format.format(tmpCalendarTo.getTime()));
			}
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		if (tmpRespCode.equals("-1")) {
			retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
			retAts.writeStart();// 写返回ATS的统一头部信息
			retAts.writeRespCode(tmpRespCode);
			retAts.writeRespInfo(tmpRespInfo);
		} else if (retAts.getWrittenRdCount() == 0) {
			retAts.writeRespCode("0");
			retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
		}
		retAts.writeEnd();
		return retAts;
	}
}
